//
//  CounterTowViewController.m
//  GoodsCountingCounter
//
//  Created by iOSdev on 2024/7/23.
//

#import "CounterTowViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "Colours/Colours.h"
#import "UDSaveCountData.h"

#define MAS_SHORTHAND
#define MAS_SHORTHAND_GLOBALS
#import "Masonry.h"

@interface CounterTowViewController (){
    CGFloat height;
}

@property (nonatomic, strong) UILabel *Instructions;
@property (nonatomic, strong) UILabel *lbCounting;


@property (nonatomic, strong) UIButton *btStart;
@property (nonatomic, assign) NSInteger currNO;


@end

@implementation CounterTowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationItem.title = @"Tow count";
    self.currNO = 0;
    
    [self addBackgroudAndTips];
    height = self.view.frame.size.height;
    // 初始化倒计时标签
    self.lbCounting = [[UILabel alloc] init];
    self.lbCounting.alpha = 0.9;
//    self.lbCounting.layer.cornerRadius = 10.0;
//    self.lbCounting.layer.masksToBounds = YES;
    self.lbCounting.textAlignment = NSTextAlignmentCenter;
    self.lbCounting.textColor = [UIColor orangeColor];
    self.lbCounting.font = [UIFont boldSystemFontOfSize:80];
    self.lbCounting.backgroundColor = UIColor.blackColor;
    [self.view addSubview:self.lbCounting];
    [self.lbCounting makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.left).offset(10);
        make.top.equalTo(self.Instructions.bottom).offset(20);
        make.right.equalTo(self.view.right).offset(-10);
        make.height.equalTo(self->height*0.18);
    }];
    
    [self addAllButton];

    [self updateCountingLabel];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
}


- (void)addBackgroudAndTips {
    UIImageView *bgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"backgroudoo"]];
    bgImage.alpha = 0.5;
    [self.view addSubview:bgImage];
    [bgImage makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    self.Instructions = [[UILabel alloc] init];
    self.Instructions.layer.cornerRadius = 5.0;
    self.Instructions.layer.masksToBounds = YES;
    self.Instructions.backgroundColor = UIColor.snowColor;
    self.Instructions.numberOfLines = 0;
    self.Instructions.textColor = UIColor.blackColor;
    self.Instructions.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:self.Instructions];
    [self.Instructions makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.left).offset(20);
        make.top.equalTo(self.view.top).offset(10);
        make.right.equalTo(self.view.right).offset(-20);
        make.height.equalTo(40);
    }];
    self.Instructions.text = @"The current page function is two counts at a time.";
}

- (void)addAllButton {
    // 创建并配置开始按钮
    self.btStart = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.btStart.alpha = 0.9;
    self.btStart.layer.cornerRadius = height*0.1;
    self.btStart.layer.masksToBounds = YES;
    [self.btStart setBackgroundColor:UIColor.systemBlueColor];
    self.btStart.titleLabel.font = [UIFont systemFontOfSize:30];
    [self.btStart setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [self.btStart setTitleColor:UIColor.grayColor forState:UIControlStateDisabled];
    [self.btStart setTitle:@"Counting" forState:UIControlStateNormal];
    [self.btStart addTarget:self action:@selector(clickedEventCounting:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btStart];
    [self.btStart makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lbCounting.bottom).offset(30);
        make.centerX.equalTo(self.view);
        make.width.height.equalTo(self->height*0.2);
    }];
    
    
    CGFloat wideView = self.view.bounds.size.width;
    UIButton *btReset = [UIButton buttonWithType:UIButtonTypeSystem];
    btReset.layer.cornerRadius = 7;
    btReset.layer.masksToBounds = YES;
    [btReset setTitle:@"Reset" forState:UIControlStateNormal];
    [btReset setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btReset setBackgroundColor:UIColor.moneyGreenColor];
//    [btReset setBackgroundImage:[UIImage imageNamed:@"blue"] forState:UIControlStateNormal];
    [self.view addSubview:btReset];
    [btReset addTarget:self action:@selector(clickedEventReset:) forControlEvents:UIControlEventTouchUpInside];
    [btReset makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.btStart.bottom).offset(30);
        make.left.equalTo(self.view.left).offset(20);
        make.right.equalTo(self.view.right).offset(-20);
        make.height.equalTo(50);
    }];
    
    UIButton *btSave = [UIButton buttonWithType:UIButtonTypeSystem];
    btSave.layer.cornerRadius = 7;
    btSave.layer.masksToBounds = YES;
    [btSave setTitle:@"Save" forState:UIControlStateNormal];
    [btSave setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btSave setBackgroundColor:UIColor.orangeColor];
//    [btSave setBackgroundImage:[UIImage imageNamed:@"blue"] forState:UIControlStateNormal];
    [self.view addSubview:btSave];
    [btSave addTarget:self action:@selector(clickedEventSave:) forControlEvents:UIControlEventTouchUpInside];
    [btSave makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(btReset.bottom).offset(20);
        make.left.equalTo(self.view.left).offset(20);
        make.right.equalTo(self.view.right).offset(-20);
        make.height.equalTo(50);
    }];
}


- (void)clickedEventCounting:(UIButton *) sender{
    self.currNO++;
    // 重置计时器代码
    [self updateCountingLabel];
}

- (void)clickedEventReset:(UIButton *)sender {
    UIAlertController *alertController = [UIAlertController
                                      alertControllerWithTitle:@"Alert"
                                                       message:@"Reset Count and the value will return to zero"
                                                   preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"OK"
                                                       style:UIAlertActionStyleDefault
                                                    handler:^(UIAlertAction *action) {
        self.currNO = 0;
        // 重置计时器代码
        [self updateCountingLabel];
    }];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
                                                           style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction *action) {
                                                         
    }];

    [alertController addAction:confirmAction];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];

}

- (void)clickedEventSave:(UIButton *)sender {
    UIAlertController *alertController = [UIAlertController
                                      alertControllerWithTitle:@"Save Count"
                                                       message:@"Save Result and Reset Count"
                                                   preferredStyle:UIAlertControllerStyleAlert];
    // 添加用户名输入框
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        // 给输入框设置一些信息
        NSInteger count = [UDSaveCountData getAllCountDataCount]+1;
        textField.placeholder = @"Name";
        textField.text = [NSString stringWithFormat:@"Count index%ld",count];
        textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        textField.textAlignment = NSTextAlignmentCenter;
    }];

    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"OK"
                                                       style:UIAlertActionStyleDefault
                                                    handler:^(UIAlertAction *action) {
        NSString *nameStr = [alertController.textFields[0] text];
        NSString *string = [NSString stringWithFormat:@"%@ \nTow count NO:%ld\n Counting Time:%@",nameStr,self.currNO,[self getCurrentDateTime]];
        [UDSaveCountData saveCountData:string];
        
        self.currNO = 0;
        // 重置计时器代码
        [self updateCountingLabel];
    }];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
                                                           style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction *action) {
                                                         
    }];

    [alertController addAction:confirmAction];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (NSString *)getCurrentDateTime {
    // 获取当前时间
    NSDate *currentDate = [NSDate date];
    // 创建日期格式化器
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    // 设置日期格式。这里设置的是常见的日期和时间格式，你可以根据需要调整。
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    // 使用日期格式化器将当前日期转换为字符串
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    // 打印格式化后的当前时间
    NSLog(@"格式化后的当前时间: %@", dateString);
    return dateString;
}

- (void)saveGoodsCount {
    NSString *string = [NSString stringWithFormat:@"Tow count NO:%ld\n Counting Time:%@",self.currNO,[self getCurrentDateTime]];
    [UDSaveCountData saveCountData:string];
}

- (void)updateCountingLabel {
    self.lbCounting.text = [NSString stringWithFormat:@"%02ld",self.currNO];
}

@end
